<template>
  <el-tooltip
    content="复制"
  >
    <el-icon
      color="blue"
      @click="onCopy"
    >
      <CopyDocument />
    </el-icon>
  </el-tooltip>
</template>

<script setup>
import { ElMessage } from 'element-plus'

const props = defineProps({
  text: {
    type: String,
    default: ''
  }
})

const onCopy = async (v) => {
  const textArea = document.createElement('textArea')
  textArea.value = props.text
  textArea.style.width = 0
  textArea.style.position = 'fixed'
  textArea.style.left = '-999px'
  textArea.style.top = '10px'
  document.body.appendChild(textArea)
  textArea.select()
  document.execCommand('copy')
  // 移除元素
  document.body.removeChild(textArea)
  ElMessage.success('复制成功')
}
</script>
